package net.yunzhanyi.client.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import net.yunzhanyi.client.constant.IndexConstant;
import net.yunzhanyi.client.domain.dto.PoetryDto;
import net.yunzhanyi.client.domain.vo.PageVo;
import net.yunzhanyi.client.service.HotWordsService;
import net.yunzhanyi.client.service.PoetryService;
import net.yunzhanyi.common.redis.service.RedisService;
import net.yunzhanyi.generic.domain.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author bestct
 * @date 2022/9/27
 * @type 类
 */
@Controller
public class IndexController {

    @Autowired
    private PoetryService poetryService;

    @Autowired
    private RedisService redisService;


    @Autowired
    private HotWordsService hotWordsService;

    @PostConstruct
    private void init() {
        List<Object> cacheList = redisService.getCacheList(IndexConstant.REDIS_INDEX_TAGS);
        if (cacheList == null || cacheList.size() == 0) {
            List<Tag> tagVos = poetryService.initTag();
            tagVos.add(0, new Tag(0, "全部"));
            redisService.setCacheList(IndexConstant.REDIS_INDEX_TAGS, tagVos);
        }
        cacheList = redisService.getCacheList(IndexConstant.REDIS_INDEX_DYNASTIES);
        if (cacheList == null || cacheList.size() == 0) {
            List<Dynasty> dynasties = poetryService.initDynasty();
            dynasties.add(0, new Dynasty(0, "全部"));
            redisService.setCacheList(IndexConstant.REDIS_INDEX_DYNASTIES, dynasties);
        }
        cacheList = redisService.getCacheList(IndexConstant.REDIS_INDEX_PARTS);
        if (cacheList == null || cacheList.size() == 0) {
            List<Part> parts = poetryService.searchPartRandom(5);
            redisService.setCacheList(IndexConstant.REDIS_INDEX_PARTS, parts);
        }

        cacheList = redisService.getCacheList(IndexConstant.REDIS_INDEX_AUTHORS);
        if (cacheList == null || cacheList.size() == 0) {
            List<Author> authors = poetryService.searchAuthorRandom(30);
            redisService.setCacheList(IndexConstant.REDIS_INDEX_AUTHORS, authors);
        }
        cacheList = redisService.getCacheList(IndexConstant.REDIS_INDEX_HOT_AUTHORS);
        if (cacheList == null || cacheList.size() == 0) {
            List<Author> authors = JSON.parseArray("[{\"authorName\":\"李白\",\"id\":4265},{\"authorName\":\"杜甫\",\"id\":4266},{\"authorName\":\"白居易\",\"id\":4267},{\"authorName\":\"辛弃疾\",\"id\":6736},{\"authorName\":\"王维\",\"id\":4276},{\"authorName\":\"李清照\",\"id\":6744},{\"authorName\":\"陆游\",\"id\":6748},{\"authorName\":\"王勃\",\"id\":4283},{\"authorName\":\"毛泽东\",\"id\":22835},{\"authorName\":\"曹操\",\"id\":3158},{\"authorName\":\"韩愈\",\"id\":4284},{\"authorName\":\"屈原\",\"id\":4001},{\"authorName\":\"曹植\",\"id\":3160},{\"authorName\":\"张九龄\",\"id\":4291},{\"authorName\":\"岑参\",\"id\":4285},{\"authorName\":\"李煜\",\"id\":3264},{\"authorName\":\"韦庄\",\"id\":4288},{\"authorName\":\"王建\",\"id\":4320},{\"authorName\":\"范仲淹\",\"id\":6747},{\"authorName\":\"岳飞\",\"id\":6756},{\"authorName\":\"马致远\",\"id\":3333},{\"authorName\":\"陈子昂\",\"id\":4277},{\"authorName\":\"张若虚\",\"id\":4310},{\"authorName\":\"郑燮\",\"id\":17097},{\"authorName\":\"龚自珍\",\"id\":17095},{\"authorName\":\"崔颢\",\"id\":4300},{\"authorName\":\"冯延巳\",\"id\":3265},{\"authorName\":\"刘长卿\",\"id\":4294},{\"authorName\":\"杨万里\",\"id\":6743},{\"authorName\":\"赵师秀\",\"id\":6820},{\"authorName\":\"贾岛\",\"id\":4279},{\"authorName\":\"杨炯\",\"id\":4304},{\"authorName\":\"睢景臣\",\"id\":3339},{\"authorName\":\"王绩\",\"id\":4332},{\"authorName\":\"晏殊\",\"id\":6745},{\"authorName\":\"柳宗元\",\"id\":4280},{\"authorName\":\"梅尧臣\",\"id\":6741},{\"authorName\":\"高鼎\",\"id\":17143},{\"authorName\":\"黄庭坚\",\"id\":6738}]", Author.class);
            redisService.setCacheList(IndexConstant.REDIS_INDEX_HOT_AUTHORS, authors);
        }
        cacheList = redisService.getCacheList(IndexConstant.REDIS_INDEX_POETRIES);
        if (cacheList == null || cacheList.size() == 0) {
            List<Poetry> poetry = poetryService.searchPoetryRandom(40);
            redisService.setCacheList(IndexConstant.REDIS_INDEX_POETRIES, poetry);
        }
        Object cacheObject = redisService.getCacheObject(IndexConstant.REDIS_INDEX_POETRY);
        if (cacheObject == null) {
            Poetry poetry=poetryService.getPoetryByRandom();
            redisService.setCacheObject(IndexConstant.REDIS_INDEX_POETRY,poetry);
        }
    }

    @GetMapping({"/index", "/"})
    public String index(Model model) {
        List<Part> partList = redisService.getCacheList(IndexConstant.REDIS_INDEX_PARTS);
        List<Author> authorList = redisService.getCacheList(IndexConstant.REDIS_INDEX_AUTHORS);
        List<Author> hotAuthorList = redisService.getCacheList(IndexConstant.REDIS_INDEX_HOT_AUTHORS);
        List<Poetry> poetryList = redisService.getCacheList(IndexConstant.REDIS_INDEX_POETRIES);
        Poetry poetry =redisService.getCacheObject(IndexConstant.REDIS_INDEX_POETRY);
        model.addAttribute("partList", partList);
        model.addAttribute("authorList", authorList);
        model.addAttribute("hotAuthorList", hotAuthorList);
        model.addAttribute("poetryList", poetryList);
        model.addAttribute("indexPoetry",poetry);
        model.addAttribute("hotWordList",hotWordsService.getHotWord(10));
        return "index";
    }

    @GetMapping("/index/poetry")
    public String indexPoetry(
            @RequestParam(value = "dynasty", defaultValue = "0") Integer dynasty,
            @RequestParam(value = "tag", defaultValue = "0") Integer tagId,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            Model model) {

        PageInfo<Poetry> poetryPageInfo = poetryService.indexPoetry(dynasty, tagId, pageNum, pageSize);
        PageVo<PoetryDto> poetryPageVo = poetryService.copyPageInfo(poetryPageInfo);
        List<Tag> tagVos = redisService.getCacheList(IndexConstant.REDIS_INDEX_TAGS);
        List<Tag> tags = poetryService.checkTagId(tagId, tagVos);
        model.addAttribute("dataInfo", poetryPageVo);
        model.addAttribute("tags", tags);
        List<Dynasty> dynasties = redisService.getCacheList(IndexConstant.REDIS_INDEX_DYNASTIES);
        model.addAttribute("dynasties", dynasties);
        model.addAttribute("tagId", tagId);
        model.addAttribute("indexDynasty", dynasty);
        return "poetry-index";
    }

    @GetMapping("/index/author")
    public String indexAuthor(
            @RequestParam(value = "dynasty", defaultValue = "0") Integer dynasty,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            Model model) {
        PageInfo<Author> authorPageInfo = poetryService.indexAuthor(dynasty, pageNum, pageSize);
        model.addAttribute("dataInfo", authorPageInfo);
        List<Dynasty> dynasties = redisService.getCacheList(IndexConstant.REDIS_INDEX_DYNASTIES);
        model.addAttribute("dynasties", dynasties);
        model.addAttribute("indexDynasty", dynasty);
        return "author-index";
    }

}
